package com.aaa.car.service.impl;

import com.aaa.car.entity.CarType;
import com.aaa.car.mapper.CarTypeMapper;
import com.aaa.car.service.CarTypeService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.entity.DictData;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CarType)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-19 19:35:44
 */
@Service("carTypeService")
public class CarTypeServiceImpl implements CarTypeService {
    @Resource
    private CarTypeMapper carTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CarType queryById(Integer id) {
        return this.carTypeMapper.queryById(id);
    }

    /**
     * 查询指定行数据
     *
     * @param
     * @param
     * @return 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, CarType carType) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<CarType> pageInfo = new PageInfo<>(carTypeMapper.queryAll(carType));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param carType 实例对象
     * @return 对象列表
     */
    @Override
    public List<CarType> queryAll(CarType carType) {
        return this.carTypeMapper.queryAll(carType);
    }

    /**
     * 新增数据
     *
     * @param carType 实例对象
     * @return 实例对象
     */
    @Override
    public CarType insert(CarType carType) {
        this.carTypeMapper.insert(carType);
        return carType;
    }

    /**
     * 修改数据
     *
     * @param carType 实例对象
     * @return 实例对象
     */
    @Override
    public CarType update(CarType carType) {
        this.carTypeMapper.update(carType);
        return this.queryById(carType.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.carTypeMapper.deleteById(id) > 0;
    }
}